/*
 * This file (AxActorsDlg.java) is protected by the GNU GPL licence (v2). 
 * Please read the license.txt attached for further informations. 
 * 
 */

package artx.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import alkwarel.objects.AkActor;
import artx.objects.AxManager;
import artx.objects.AxObject;
import artx.utils.AxLog;

/**
 * Standard dialog to select an Actor.
 * 
 * @author Xavier Gouchet
 * 
 */
public class AxActorsDlg extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JPanel mainPanel = null;

	private JScrollPane scrollPane = null;

	private JPanel contentPanel = null;

	private AxManager actorsManager = null;

	/** the name of the selected actor */
	public String selected = null;

	/**
	 * Standard constructor.
	 * 
	 * @param owner
	 *            the master window calling this dialog
	 * @param title
	 *            the title of the dialog
	 * @param actors
	 *            the actors manager from which the dialog will be built
	 */
	public AxActorsDlg(Frame owner, String title, AxManager actors) {
		super(owner);
		this.setTitle(title);
		this.actorsManager = actors;
		this.setContentPane(getMainPanel());
		this.pack();
	}

	private JPanel getMainPanel() {
		if (this.mainPanel == null) {
			this.mainPanel = new JPanel();
			this.mainPanel.setLayout(new FlowLayout());
			this.mainPanel.add(this.getScrollPane());
		}
		return this.mainPanel;
	}

	private JScrollPane getScrollPane() {
		if (this.scrollPane == null) {
			this.scrollPane = new JScrollPane();
			this.scrollPane.setPreferredSize(new Dimension(250, 300));
			this.scrollPane
					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
			this.scrollPane
					.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			this.scrollPane.setViewportView(this.getContentPanel());
		}
		return this.scrollPane;
	}

	private JPanel getContentPanel() {
		if (this.contentPanel == null) {
			this.contentPanel = new JPanel();
			this.contentPanel.setLayout(new GridBagLayout());

			this.BuildContent();
		}
		return this.contentPanel;
	}

	private void BuildContent() {
		if (this.actorsManager == null)
			return;
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridy = 0;
		gbc.insets = new Insets(2, 2, 2, 2);
		Dimension btnDim = new Dimension(100, 20);
		Dimension lblDim = new Dimension(135, 20);
		Dimension descDim = new Dimension(240, 30);

		ArrayList<AxObject> actors = this.actorsManager.GetElements();
		for (int i = 0; i < actors.size(); i++) {
			AkActor actor = (AkActor) actors.get(i);
			if (actor != null) {
				gbc.gridx = 0;
				gbc.gridwidth = 1;
				gbc.fill = GridBagConstraints.NONE;
				String name = actor.GetName();
				JButton btn = new JButton(name.toUpperCase().charAt(0)
						+ name.substring(1));
				btn.setPreferredSize(btnDim);
				btn.addActionListener(this);
				this.contentPanel.add(btn, gbc);

				gbc.gridx = 1;
				gbc.fill = GridBagConstraints.BOTH;
				JLabel lbl = new JLabel("by " + actor.GetAuthor());
				lbl.setPreferredSize(lblDim);
				this.contentPanel.add(lbl, gbc);
				gbc.gridy++;

				gbc.gridx = 0;
				gbc.gridwidth = 2;
				gbc.fill = GridBagConstraints.BOTH;
				JTextPane desc = new JTextPane();
				desc.setText(actor.GetDescription());
				desc.setEditable(false);
				desc.setPreferredSize(descDim);
				desc.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0,
						Color.black));
				this.contentPanel.add(desc, gbc);
				gbc.gridy++;
			}
		}
	}

	/**
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		AxLog.Debug("AxActorsDlg::actionPerformed =>> Selecting actor "
				+ e.getActionCommand().toLowerCase());
		this.setVisible(false);
		this.selected = e.getActionCommand().toLowerCase();
	}

}
